package models;

import java.util.*;

import javax.persistence.*;

import com.avaje.ebean.Expr;

import play.db.ebean.*;
import play.data.format.*;
import play.data.validation.*;


/**
 * User entity managed by Ebean
 */
@Entity
@Table(name="user")
public class User extends Model {

	public static final long serialVersionUID = 1L;

	@Id
	@Column(name="id")
	public long id;

    @Column(name="email_address")
    public String email;

    @Column(name="name")
    public String name;

    @Column(name="provider")
    public String provider;

    @Column(name="userid")
    public String userid;

    @Column(name="phone_no")
    public String phoneNumber;

    @Column(name="user_type")
    public String userType;

    @Column(name="client_id")
    public String clientId;

    @Column(name="created")
    public String created=null;
    
    @Column(name="approved")
    public String approved;
    
    @Column(name="gcm_regid")
    public String gcmRegistrationId;
    // -- Queries

    public static Model.Finder<String,User> find = new Model.Finder(String.class, User.class);

    /**
     * Authenticate a User.
     */
    public static User authenticate(String provider, String userId) {
        return find.where()
            .eq("provider", provider)
            .eq("userid", userId)
            .findUnique();
    }


    public static User checkDummyUser(String email) {
        return find.where()
            .eq("email_address", email)
            .eq("provider","dummy")
            .eq("userid", email)
            .findUnique();
    }

    /**
     * Get Doctors.
     */
    public static List<User> getDoctors() {
        return find.where()
            .eq("user_type", "D")
            .findList();
    }
    
    /**
     * Get User Details.
     */
    public static User getUserDetails(long userId) {
        return find.where()
            .eq("id", userId)
            .findUnique();
    }

    public static List<User> getDoctorName(String name, long specialityId) {
    	List<User> doctors= new ArrayList<User>();
    	if(specialityId > 0){
    		doctors=find.where()
            .eq("specialityId", specialityId)
            .eq("user_type", "D")
            .like("name", "%"+name+"%")
            .findList();
    	}else{
    		doctors=find.where()
            .eq("user_type", "D")
            .like("name", "%"+name+"%")
            .findList();
    	}
        return doctors;
    }

    public static List<User> getPatientDetails(String name, String clientId) {
    	List<User> patients= new ArrayList<User>();
    	if(null != name && !"".equals(name)){
    		patients=find.where()
            .eq("user_type", "P")
            .like("name", "%"+name+"%")
            .findList();
    	}else if(null != clientId && !"".equals(clientId)){
    		patients=find.where()
            .eq("user_type", "P")
            .like("client_id", "%"+clientId+"%")
            .findList();
    	}
        return patients;
    }
    
    public static List<User> getUnapprovedUsers() {
    	ArrayList<String> list = new ArrayList<String>() {{
    	    add("D");
    	    add("S");
    	    add("A");
    	}};
        return find.where()
        		.in("user_type", list)
        		.isNull("approved")
        		.findList();
    }
    
    public static List<User> getMatchingDoctorNames(String name){
    	List<User> users = new ArrayList<User>();
    	StringBuilder s = new StringBuilder();
    	s.append("(soundex(SUBSTRING(name,1, LOCATE(' ', name)) )= soundex(SUBSTRING('")
    	.append(name)
    	.append("' , 1,LOCATE(' ', '")
    	.append(name)
    	.append("') ))) and ( soundex(SUBSTRING(name, LOCATE(' ', name) +1,length(SUBSTRING('")
    	.append(name)
    	.append("', LOCATE(' ', '")
    	.append(name)
    	.append("' ) +1)))) =soundex(SUBSTRING('")
    	.append(name)
    	.append("', LOCATE(' ', '")
    	.append(name)
    	.append("' ) +1)))");
    	
    	users = find
    			.where()
    			.add(Expr.raw(s.toString()))
				.findList();
    	return users;
    }
    
    public static List<User> getPatients(String term){
    	return find
    			.where()
    			.eq("user_type","P")
    			.like("name", term+"%")
    			.findList();
    		
    }
    
    public static List<User> getAllDoctors(){
    	return find
    			.where()
    			.eq("user_type","D")
    			.add(Expr.raw("length(userid)>0"))
    			.findList();
    		
    }
}

